package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/12 14:52
 * https://leetcode-cn.com/problems/house-robber/
 */
public class Title198 {

    /**
     * dp[j]表示光顾1-j号房子，偷窃的最大金额
     * 递推：dp[j] = Math.max(dp[j-1], dp[i-2] + nums[i])
     * 初始化：dp[0] = 0  dp[1] = nums[1]
     * @param nums
     * @return
     */
    public int rob(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        dp[1] = Math.max(nums[0], nums[1]);
        for (int i = 2; i < nums.length; i++) {
            dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
        }
        return dp[nums.length - 1];
    }



    public static void main(String[] args) {

    }
}
